package com.piece.core.verifycode.processor;

import com.piece.core.framework.constant.ExceptionAuthConstants;
import com.piece.core.framework.util.basic.I18nUtil;
import com.piece.core.verifycode.enums.ValidateCodeType;
import com.piece.core.verifycode.exception.ValidateCodeException;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Map;

/**
 * 校验码处理器管理器
 */
public class ValidateCodeProcessorHolder {

    @Autowired
    private Map<String, ValidateCodeProcessor> validateCodeProcessors;

    public ValidateCodeProcessor findValidateCodeProcessor(ValidateCodeType type) {
        return findValidateCodeProcessor(type.toString().toLowerCase());
    }

    public ValidateCodeProcessor findValidateCodeProcessor(String type) {
        String name = type.toLowerCase() + ValidateCodeProcessor.class.getSimpleName();
        ValidateCodeProcessor processor = validateCodeProcessors.get(name);
        if (null == processor) {
            throw new ValidateCodeException(I18nUtil.message(ExceptionAuthConstants.VALIDATE_CODE_UNKNOW, name));
        }
        return processor;
    }
}
